<template>
  <section>
    {{ records }}
    <el-table
      :data="pageList"
      tooltip-effect="dark"
      class="table-style"
      height="550"
      v-loading="loading"
      border
      :key="total"
    >
      <el-table-column label="#" type="index"></el-table-column>
      <template v-for="(column, index) in columns">
        <table-column-import
          :key="index"
          v-bind="column"
          :type="column.type"
          @updated="handleUpdated(column, arguments)"
        />
      </template>
      <el-table-column label="状态" fixed="right" width="80px">
        <template slot-scope="scope">
          <validate-status :status="scope.row.validatorFlag" />
        </template>
      </el-table-column>
      <el-table-column
        :label="!isResult ? '提示' : '原因'"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <tip-message :value="scope.row.errors" />
        </template>
      </el-table-column>
      <el-table-column width="150" fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="default"
            size="mini"
            v-show="!isResult && !available(scope.row)"
            @click="handleRemove(scope.$index)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="pageSizeOptions"
      :page-size.sync="size"
      :current-page.sync="page"
      @size-change="currentPage"
      @current-change="currentPage"
    >
    </el-pagination>
  </section>
</template>

<script>
import TableColumnImport from "./TableColumnImport.vue";
import ValidateStatus from "./ValidateStatus.vue";
import TipMessage from "./TipMessage.vue";
import upload from "./UploadExcel.vue";

export default {
  components: { TableColumnImport, ValidateStatus, TipMessage, upload },
  name: "TableBoxImport",
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    isResult: { type: Boolean, default: false },
    api: {
      type: Object
    },
    records: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      size: 10,
      page: 1,
      total: 0,
      pageList: [],
      loading: false,
      pageSizeOptions: [10, 20, 50, 100],
      cloneRecords: []
    };
  },
  computed: {
    available() {
      return (row) => row.validatorFlag || row.$changed;
    },
    errorRecords() {
      return this.cloneRecords.filter((record) => !this.available(record));
    },
    errorSize() {
      return this.errorRecords.length;
    },
    submitDisabled() {
      return this.errorSize > 0;
    },
    column() {
      return (name) => this.columns.filter((column) => column.name === name);
    }
  },
  watch: {
    records: {
      immediate: true,
      handler(val) {
        this.cloneRecords = this.records
          .map((record) => {
            return {
              ...record,
              disabled: this.available(record),
              sort: record.validatorFlag ? 1 : 0
            };
          })
          .sort((a, b) => a.sort - b.sort);
        this.currentPage();
      }
    },
    isResult: {
      immediate: true,
      handler(val) {
        return val;
      }
    }
  },
  created() {
    this.$set(
      this,
      "cloneRecords",
      this.records
        .map((record) => {
          return {
            ...record,
            disabled: this.available(record),
            sort: record.validatorFlag ? 1 : 0
          };
        })
        .sort((a, b) => a.sort - b.sort)
    );
    // this.cloneRecords = this.records
    //   .map(record => {
    //     return {
    //       ...record,
    //       disabled: this.available(record),
    //       sort: record.validatorFlag ? 1 : 0
    //     }
    //   })
    //   .sort((a, b) => a.sort - b.sort)
    this.currentPage();
  },

  methods: {
    currentPage() {
      this.total = this.cloneRecords.length;
      this.pageList = this.cloneRecords.slice(
        (this.page - 1) * this.size,
        this.page * this.size
      );
    },
    upload(data) {
      this.cloneRecords = data;
      this.currentPage();
      this.isResult = false;
    },
    handleUpdated(column, [{ index, value }]) {
      let record = this.cloneRecords[index];
      this.$set(record, "$changed", true);
      this.$emit("submitDisabled", this.submitDisabled);
      // this.$emit("update:records", this.cloneRecords)
    },

    handleRemove(index) {
      this.cloneRecords.splice(index, 1);
      this.currentPage();
    }
  }
};
</script>
